﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class ToDoListControl : BaseUserControl
    {
        string FieldName_STATETEXT = "TSTATETEXT";

        private DateTime currQueryTime = DateTime.Now;
        /// <summary>
        /// 待办事项
        /// </summary>
        private DataSet dsTodoListUnfiniushed = new DataSet();
        /// <summary>
        /// 已完结
        /// </summary>
        private DataSet dsTodoListFinush = new DataSet();
        /// <summary>
        /// 超期的待办事项
        /// </summary>
        private DataSet dsTodoListUnDate = new DataSet();
        
        public ToDoListControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化控件并刷新数据
        /// </summary>
        public void InitControl(DateTime queryStartTime)
        {
            string error = string.Empty;

            DataSet dsEmployee ;
            if (!BussinessProxyManager.Instance.QueryEmployeeState("", out dsEmployee, out error))
            {
                MsgBox.MsgInfo("初始化员工信息错误：" + error);
                return;
            }
            currQueryTime = new DateTime(queryStartTime.Ticks);
            
            #region 进行中的待办事项包括（未开始状态、和正在进行状态的）
            bool bFlag = BussinessProxyManager.Instance.QueryTodoList("", out dsTodoListUnfiniushed, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("初始化代办事项错误：" + error);
                return;
            }

            dsTodoListUnfiniushed.Tables[0].Columns.Add(new DataColumn(DBEntityEmployee.FieldName.ENAME, typeof(string)));
            foreach (DataRow dr in dsTodoListUnfiniushed.Tables[0].Rows)
            {
                if (dr[DBEntityEmployee.FieldName.EMPLOYEEID] == System.DBNull.Value)
                {
                    continue;
                }
                DataRow[] drEmployee = dsEmployee.Tables[0].Select(DBEntityEmployee.FieldName.EMPLOYEEID + " = " + dr[DBEntityEmployee.FieldName.EMPLOYEEID].ToString());
                if (drEmployee.Length == 0)
                {
                    continue;
                }
                dr[DBEntityEmployee.FieldName.ENAME] = drEmployee[0][DBEntityEmployee.FieldName.ENAME];
            }

            DataColumn dc = new DataColumn(FieldName_STATETEXT);
            dsTodoListUnfiniushed.Tables[0].Columns.Add(dc);
            foreach (DataRow dr in dsTodoListUnfiniushed.Tables[0].Rows)
            {
                string sValue = string.Empty;
                switch (dr[DBEntityToDoList.FieldName.TODOSTATE].ToString())
                {
                    case DBEntityToDoList.ENUM_TODOSTATE.T0I:
                        sValue = DBEntityToDoList.ENUM_TODOSTATE.T0I_CHINESE;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0N:
                        sValue = DBEntityToDoList.ENUM_TODOSTATE.T0N_CHINESE;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0T:
                        sValue = DBEntityToDoList.ENUM_TODOSTATE.T0T_CHINESE;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0V:
                        sValue = DBEntityToDoList.ENUM_TODOSTATE.T0V_CHINESE;
                        break;
                }
                dr[FieldName_STATETEXT] = sValue;
            }

            dsTodoListFinush = dsTodoListUnfiniushed.Clone();
            dsTodoListUnDate = dsTodoListUnfiniushed.Clone(); ;

            gvToDoList.BeginUpdate();
            try
            {
                string filter = string.Format("{0}='{1}' OR {0}='{2}' OR {0} = '{3}'", 
                    DBEntityToDoList.FieldName.TODOSTATE, 
                    DBEntityToDoList.ENUM_TODOSTATE.T0T, 
                    DBEntityToDoList.ENUM_TODOSTATE.T0V,
                    DBEntityToDoList.ENUM_TODOSTATE.T0N);

                DataRow[] drTodoList = dsTodoListUnfiniushed.Tables[0].Select(filter);
                foreach (DataRow dr in drTodoList)
                {//删除已经完成的，和过期的
                    if (dr[DBEntityToDoList.FieldName.TODOSTATE].ToString().Equals(DBEntityToDoList.ENUM_TODOSTATE.T0V))
                    {
                        //已经完成
                        dsTodoListFinush.Tables[0].ImportRow(dr);
                    }
                    else if (dr[DBEntityToDoList.FieldName.TODOSTATE].ToString().Equals(DBEntityToDoList.ENUM_TODOSTATE.T0T))
                    {
                        //超期的
                        dsTodoListUnDate.Tables[0].ImportRow(dr);
                    }
                    dr.Delete();
                }

                dsTodoListUnfiniushed.AcceptChanges();
                dsTodoListUnDate.AcceptChanges();
                dsTodoListFinush.AcceptChanges();

                gridToDoList.DataSource = dsTodoListUnfiniushed.Tables[0];
                gvToDoList.ExpandAllGroups();
            }
            finally
            {
                gvToDoList.EndUpdate();
            }
            #endregion

            #region 过期的代办事项
            gvToDoListUnFinush.BeginUpdate();
            try
            {
                gridToDoListUnFinush.DataSource = dsTodoListUnDate.Tables[0];
                gvToDoListUnFinush.ExpandAllGroups();
            }
            finally
            {
                gvToDoListUnFinush.EndUpdate();
            }
            #endregion

            #region 已经完成的待办事项
            try
            {
                gvToDoListFinush.BeginUpdate();
                gridToDoListFinush.DataSource = dsTodoListFinush.Tables[0];
                gvToDoListFinush.ExpandAllGroups();
            }
            finally
            {
                gvToDoListFinush.EndUpdate();
            }
            #endregion

        }

        private void gvToDoList_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView gv = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
            if (gv.FocusedRowHandle < 0)
                return;

            string error = string.Empty;
            DataSet dsEmployee = new DataSet();
            if(!GetEmployeeInfo(out dsEmployee, out error)){
                MsgBox.MsgInfo("获取员工数据错误：" + error);
                return;
            }
            DataRow drToDoList = gv.GetDataRow(gv.FocusedRowHandle);
            DBEntityToDoList todoListInfo = new DBEntityToDoList(drToDoList);
            frmToDoListSimpInfo frmlistInfo = new frmToDoListSimpInfo(null, todoListInfo);
            frmlistInfo.Mode = ZhimeiSoft.Framework.Common.Enums.ControlClassModeEnum.emEdit;
            if (frmlistInfo.ShowDialog() == DialogResult.OK)
            {
                this.InitControl(currQueryTime);
            }

        }
        private bool GetEmployeeInfo(out DataSet dsEmployee, out string error)
        {
            return BussinessProxyManager.Instance.QueryEmployeeState(string.Empty, out dsEmployee, out error);
            
        }

        private void gvToDoListUnFinush_DoubleClick(object sender, EventArgs e)
        {
            gvToDoList_DoubleClick(sender, e);
        }

        private void gvToDoListFinush_DoubleClick(object sender, EventArgs e)
        {
            gvToDoList_DoubleClick(sender, e);
        }

        private void gvToDoList_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //if(e.RowHandle < 0)
            //    return;

            //if (e.Column.FieldName.ToUpper().Equals(DBEntityEmployee.FieldName.EMPLOYEEID))
            //{
            //    string empName = sortEmployee[Convert.ToInt16(e.CellValue)];
            //    if (empName == null)
            //        return;
            //    Rectangle r = e.Bounds;
            //    e.Style.DrawString(e.Graphics, empName, r);
            //    e.Handled = true;
            //}
        }

        private void gvToDoList_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
        {
            
            
        }

        private void gvToDoList_CustomDrawGroupPanel(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
         
        }
    }
}
